import allure
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from page.base_page import BasePage


class PlatformAccountDetail(BasePage):
    __order_management = By.XPATH, "//*[text()='订单管理']"
    __proxy_order = By.XPATH, "//*[text()='代理订单']"
    __disbursement_account = By.XPATH, "//*[text()='放款虚户']"
    __disbursement_account_detail = By.XPATH, '//*[@href="/financial/disbursementAccount/finAccount"]'
    __proxy_order_list = By.XPATH, "//*[@href = '/order/proxyOrder/proxyOrderApply']"
    __total_account_amount = By.XPATH, "//*[text()='账户总额（元）']/../p[2]"
    __available_amount = By.XPATH, "//*[text()='可用金额（元）']/../p[2]"
    __freeze_amount = By.XPATH, "//*[text()='冻结金额（元）']/../p[2]"
    __total_consumption = By.XPATH, "//*[text()='消费总额（元）']/../p[2]"
    __approve_proxy_order = By.XPATH, '//*[@href="/order/proxyOrder/proxyOrderCheck"]'

    def goto_proxy_order(self):
        from page.proxy_order_apply_page import ProxyOrderApply
        with allure.step('点击订单管理'):
            self.find_and_click(self.__order_management)
        with allure.step('点击代理订单'):
            self.find_and_click(self.__proxy_order)
        with allure.step('点击订单列表'):
            self.find_and_click(self.__proxy_order_list)
            self.wait(EC.visibility_of_element_located((By.XPATH, "//*[text()='借贷人姓名']")), time_out=10)
        return ProxyOrderApply(self.driver)

    def goto_approve_proxy(self):
        from page.approve_proxy_order_page import ApproveProxyOrderPage
        with allure.step('点击订单管理'):
            self.find_and_click(self.__order_management)
        with allure.step('点击代理订单'):
            self.find_and_click(self.__proxy_order)
        with allure.step('点击审批订单'):
            self.find_and_click(self.__approve_proxy_order)
            self.wait(EC.visibility_of_element_located((By.XPATH, "//*[text()='借贷人姓名']")), time_out=10)
        return ApproveProxyOrderPage(self.driver)

    def goto_disbursement_account_detail(self):
        from page.disbursement_account_page import DisbursementAccount
        with allure.step('点击放款虚户'):
            self.find_and_click(self.__disbursement_account)
        with allure.step('点击账户详情'):
            self.find_and_click(self.__disbursement_account_detail)
            self.wait(EC.visibility_of_element_located((By.XPATH, "//*[text()='配资方案']")), time_out=10)
        return DisbursementAccount(self.driver)

    def get_total_account_amount(self):
        with allure.step('获取平台虚户账户总额'):
            total_account_amount = self.find(self.__total_account_amount).text
        return total_account_amount

    def get_available_amount(self):
        with allure.step('获取平台虚户可用金额'):
            available_amount = self.find(self.__available_amount).text
        return available_amount

    def get_freeze_amount(self):
        with allure.step('获取平台虚户冻结金额'):
            freeze_amount = self.find(self.__freeze_amount).text
        return freeze_amount

    def get_total_consumption(self):
        with allure.step('获取平台虚户消费总额'):
            total_consumption = self.find(self.__total_consumption).text
        return total_consumption
